
import 'package:flutter/cupertino.dart';
import 'package:flutter_09_game/base/controller/base_list_more_controller.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/deposit/utils_deposit.dart';
import 'package:flutter_09_game/pages/profile/profile_bet_record/models/model_game_round_join.dart';
import 'package:flutter_09_game/pages/profile/profile_bet_record/profile_bet_record_logic.dart';
import 'package:flutter_09_game/pages/profile/profile_bet_record/utils_bet_record.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:get/get.dart';

class ProfileBetRecordListLogic extends BaseListMoreController {
  // ProfileBetRecordLogic recordLogic = Get.find<ProfileBetRecordLogic>();
  ProfileBetRecordListLogic({required this.joinType});
  final int joinType;

  RxString selectorTagRx = ''.obs;
  RxString timeRx = ''.obs;

  RxString symbolRx = ''.obs;
  RxString statusRx = ''.obs;
  RxString gameRx = ''.obs;

  TextEditingController idController = TextEditingController();

  // 日期范围相关属性
  Rx<DatePeriod?> selectedDateRange = Rx<DatePeriod?>(null);
  late ProfileBetRecordLogic recordLogic;

  ModelGameRoundJoin roundJoin = ModelGameRoundJoin(stats: [
    ModelGameRoundJoinStats(symbol: 'TRX', amount: 0, winAmount: 0, count: 0),
    ModelGameRoundJoinStats(symbol: "USDT", amount: 0, winAmount: 0, count: 0),
  ]);

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    everAll([symbolRx, statusRx, gameRx,selectorTagRx], (v) {
      refreshController.requestRefresh();
    });
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();

    selectorTagRx.value = UtilsBetRecord.options.first;
    timeRx.value = UtilsBetRecord.timeRows.first;
    symbolRx.value = UtilsBetRecord.symbolRows.first;
    statusRx.value = UtilsBetRecord.statusRows.first;
    gameRx.value = '';
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    idController.dispose();
  }

  @override
  Future<List> loadData() async {
    // TODO: implement loadData
    final dateRange = UtilsDeposit.calculateDateRange(timeRx.value,
        selectedDateRange: selectedDateRange.value);

    HttpResponse response = await HttpManager().get(API.getGameRoundJoin,
        params: {
          // 'game_id': Get.find<ProfileBetRecordLogic>()
          //     .gameActiveList
          //     .firstWhereOrNull((item) => item.game?.name == gameRx.value)
          //     ?.game
          //     ?.id ??
          //     '',
          'status': UtilsBetRecord.statusRows.indexOf(statusRx.value),
          'game_round_join_identity': idController.text,
          'symbol': symbolRx.value == UtilsBetRecord.symbolRows.first
              ? ''
              : symbolRx.value,

          'start': dateRange['start'],
          'end': dateRange['end'],
          'page': pageNum,
          'size': pageSize,
          'is_team': UtilsBetRecord.options.first == selectorTagRx.value
              ? false
              : true,
          'join_type': joinType,
        },
        fromJson: ModelGameRoundJoin.fromJson);
    if (response.isSuccess) {
      roundJoin = response.data;
    }
    return roundJoin.gameRoundJoin ?? [];
    // return super.loadData();
  }

  /// 更新时间选择
  void updateTimeSelection(String timeType) {
    if (timeType == LocaleKeys.custom.tr) {
      UtilsDeposit.showDateRangePicker(selectedDateRange.value).then((value) {
        if (value is DatePeriod) {
          selectedDateRange.value = value;
          refreshController.requestRefresh();
          timeRx.value = timeType;
        }
      });
    } else {
      timeRx.value = timeType;
      // 清除自定义日期范围
      selectedDateRange.value = null;
      refreshController.requestRefresh();
    }
  }
}